package com.matrix.auth.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.matrix.api.user.dto.*;
import com.matrix.api.user.vo.AccessTokenVO;
import com.matrix.api.user.vo.LoginSuccessVO;
import com.matrix.auth.dto.SmsSendDTO;
import com.matrix.auth.service.AuthService;
import com.matrix.common.domain.Result;
import com.matrix.common.exception.SentinelBlockExceptionHandler;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 有点甜
 * @since 2025/7/25
 */
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {

    private final AuthService authService;

    @PostMapping("/register")
    public Result<Void> register(@Valid @RequestBody UserRegisterDTO registerDTO) {
        return authService.register(registerDTO);
    }

    @PostMapping("/sms/send")
    @SentinelResource(value = "auth-sendSms", blockHandler = "handleBlockException", blockHandlerClass = SentinelBlockExceptionHandler.class)
    public Result<Void> sendSmsCode(@Valid @RequestBody SmsSendDTO smsSendDTO) {
        authService.sendVerificationCode(smsSendDTO.getPhone(), smsSendDTO.getType());
        return Result.success();
    }

    @PostMapping("/login/password")
    @SentinelResource(value = "auth-passwordLogin", blockHandler = "handleBlockException", blockHandlerClass = SentinelBlockExceptionHandler.class)
    public Result<?> passwordLogin(@Valid @RequestBody PasswordLoginDTO loginDTO) {
        return authService.passwordLogin(loginDTO);
    }

    @PostMapping("/login/mfa")
    public Result<LoginSuccessVO> smsMfaLogin(@Valid @RequestBody SmsMfaLoginDTO mfaLoginDTO) {
        return authService.smsMfaLogin(mfaLoginDTO);
    }

    @PostMapping("/login/sms")
    public Result<LoginSuccessVO> smsLogin(@Valid @RequestBody SmsLoginDTO loginDTO) {
        return authService.smsLogin(loginDTO);
    }

    @PostMapping("/refresh")
    public Result<AccessTokenVO> refreshAccessToken(@Valid @RequestBody RefreshTokenDTO refreshTokenDTO) {
        return authService.refreshAccessToken(refreshTokenDTO.getRefreshToken());
    }
}
